﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.Config;
using CNVP.Framework;

namespace CNVP.JShop.Web.Admin
{
    public partial class PicAddWaterSet : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 127)) return;//判断页面权限
            if (!IsPostBack)
            {
                txtSmallWidth.Text = UIPicAddWaterConfig.SmallWidth.Trim();
                txtMiddleWidth.Text = UIPicAddWaterConfig.MiddleWidth.Trim();
                txtBigWidth.Text = UIPicAddWaterConfig.BigWidth.Trim();
                rblIsTextWatermark.SelectedValue = UIPicAddWaterConfig.IsTextWatermark;
                txtTextWatermark.Text = UIPicAddWaterConfig.TextWatermark.Trim();
                ddlTextFont.SelectedValue = UIPicAddWaterConfig.TextFont;
                txtTextSmallFontSize.Text = UIPicAddWaterConfig.TextSmallFontSize.Trim();
                txtTextMiddleFontSize.Text = UIPicAddWaterConfig.TextMiddleFontSize.Trim();
                txtTextBigFontSize.Text = UIPicAddWaterConfig.TextBigFontSize.Trim();
                txtTextOriginalFontSize.Text = UIPicAddWaterConfig.TextOriginalFontSize.Trim();
                ddlTextFontColor.Text = UIPicAddWaterConfig.TextFontColor;
                txtTextFloatX.Text=UIPicAddWaterConfig.TextFloatX.Trim();
                txtTextFloatY.Text = UIPicAddWaterConfig.TextFloatY.Trim();
                rblIsImageWatermark.SelectedValue = UIPicAddWaterConfig.IsImageWatermark;
                if (UIPicAddWaterConfig.ImageWatermark != "")
                {
                    imgPic.Visible = true;
                    imgPic.ImageUrl = "../UploadFile/Logo/" + UIPicAddWaterConfig.ImageWatermark;
                }
                txtImageWatermark.Text = UIPicAddWaterConfig.ImageWatermark;
                txtImageFloatX.Text=UIPicAddWaterConfig.ImageFloatX.Trim();
                txtImageFloatY.Text = UIPicAddWaterConfig.ImageFloatY.Trim();
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            string _SmallWidth = txtSmallWidth.Text.Trim();
            string _MiddleWidth = txtMiddleWidth.Text.Trim();
            string _BigWidth = txtBigWidth.Text.Trim();
            string _IsTextWatermark = rblIsTextWatermark.SelectedValue;
            string _TextWatermark = txtTextWatermark.Text.Trim();
            string _TextFont = ddlTextFont.SelectedValue;
            string _TextSmallFontSize = txtTextSmallFontSize.Text.Trim();
            string _TextMiddleFontSize = txtTextMiddleFontSize.Text.Trim();
            string _TextBigFontSize = txtTextBigFontSize.Text.Trim();
            string _TextOriginalFontSize = txtTextOriginalFontSize.Text.Trim();
            string _TextFontColor = ddlTextFontColor.SelectedValue;
            string _TextFloatX = txtTextFloatX.Text.Trim();
            string _TextFloatY = txtTextFloatY.Text.Trim();
            string _IsImageWatermark = rblIsImageWatermark.SelectedValue;
            string _ImageWatermark = txtImageWatermark.Text.Trim();
            string _ImageFloatX = txtImageFloatX.Text.Trim();
            string _ImageFloatY = txtImageFloatY.Text.Trim();

            PicAddWaterConfig.SaveXmlConfig("SmallWidth", _SmallWidth);
            PicAddWaterConfig.SaveXmlConfig("MiddleWidth", _MiddleWidth);
            PicAddWaterConfig.SaveXmlConfig("BigWidth", _BigWidth);
            PicAddWaterConfig.SaveXmlConfig("IsTextWatermark", _IsTextWatermark);
            PicAddWaterConfig.SaveXmlConfig("TextWatermark", _TextWatermark);
            PicAddWaterConfig.SaveXmlConfig("TextFont", _TextFont);
            PicAddWaterConfig.SaveXmlConfig("TextSmallFontSize", _TextSmallFontSize);
            PicAddWaterConfig.SaveXmlConfig("TextMiddleFontSize", _TextMiddleFontSize);
            PicAddWaterConfig.SaveXmlConfig("TextBigFontSize", _TextBigFontSize);
            PicAddWaterConfig.SaveXmlConfig("TextOriginalFontSize", _TextOriginalFontSize);
            PicAddWaterConfig.SaveXmlConfig("TextFontColor", _TextFontColor);
            PicAddWaterConfig.SaveXmlConfig("TextFloatX", _TextFloatX);
            PicAddWaterConfig.SaveXmlConfig("TextFloatY", _TextFloatY);
            PicAddWaterConfig.SaveXmlConfig("IsImageWatermark", _IsImageWatermark);
            string newName = "";
            string root = Server.MapPath("../UploadFile/Logo/");
            if (this.fileUp.PostedFile.ContentLength != 0)
            {
                string fileName = fileUp.PostedFile.FileName;
                string imgType = fileName.Substring(fileName.LastIndexOf(".") + 1);
                if (imgType.ToLower() != "png")
                {
                    BasePage.MessageBoxGoHistory("上传的图片必须为.png！");
                    return;
                }
                newName = "Water_" + DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + imgType;
                fileUp.PostedFile.SaveAs(root + "\\" + newName);
                imgPic.ImageUrl = "../UploadFile/Logo/" + newName;
            }
            else
            {
                newName = _ImageWatermark;
            }
            PicAddWaterConfig.SaveXmlConfig("ImageWatermark", newName);
            PicAddWaterConfig.SaveXmlConfig("ImageFloatX", _ImageFloatX);
            PicAddWaterConfig.SaveXmlConfig("ImageFloatY", _ImageFloatY);
            UIPicAddWaterConfig.RefurbishCatch();//刷新缓存
            LicenseConfig.RefurbishCatch();
            if (this.fileUp.PostedFile.ContentLength != 0)
            {
                BasePage.DeleteFile(root + "\\" + txtImageWatermark.Text);
                imgPic.ImageUrl = "../UploadFile/Logo/" + newName;
            }
            txtImageWatermark.Text = newName;
            
            BasePage.MessageBox("修改成功！", "PicAddWaterSet.aspx");
        }
    }
}